# 自定义的断言类
from app_ui.lib.base.app_keywords import AppKeyWords
from Lib.common.ui_log import error_log, normal_log
from appium.webdriver.webdriver import WebDriver
import time


class AssertClass:

    def __init__(self, driver: WebDriver):
        self.kw = AppKeyWords(driver)

    def assert_by_text(self, args, assert_data, context=None):
        '''
        根据文本信息设置断言
        :param args:
        :param assert_data:
        :param context:
        :return:
        '''
        try:
            text = self.kw.get_text(args=args, context=None)
            print(text)
            assert assert_data in text, '断言失败，没有获得对应的文本信息'
        except AssertionError as e:
            error_log().debug(e)
            raise e

    def assert_by_compare_time(self, args1, args2, context=None):
        '''
        按时间进行排序后 比较两个时间判断排序是否正确
        :param args1:
        :param args2:
        :param context:
        :return:
        '''
        try:
            time1_str1 = self.kw.get_text(args=args1, context=None)
            time1 = time.mktime(time.strptime(time1_str1.strip(), '%Y-%m-%d %H:%M:%S'))
            time1_str2 = self.kw.get_text(args=args2, context=None)
            time2 = time.mktime(time.strptime(time1_str2.strip(), '%Y-%m-%d %H:%M:%S'))
            assert int(time1) > int(time2), '排序错误'
        except AssertionError as e:
            error_log().debug(e)
            raise e

    def assert_by_text_different(self, args, assert_data, context=None):
        '''
        当文本信息不相等时的断言
        :param args:
        :param assert_data:
        :param context:
        :return:
        '''
        try:
            text = self.kw.get_text(args=args, context=None)
            assert assert_data not in text, '断言失败，获取的文本信息包含断言信息'
        except AssertionError as e:
            error_log().debug(e)
            raise e

    def assert_by_text_is_null(self, args, context=None):
        '''
        判断值是否为空值
        :param args:
        :param assert_data:
        :param context:
        :return:
        '''
        try:
            text = self.kw.get_text(args=args, context=None)
            assert text is not None, '断言失败，返回的数据为空'
        except AssertionError as e:
            error_log().debug(e)
            raise e

    def assert_by_elements_text(self, args, assert_data, context=None):
        '''
        根据当前页面多个元素的text()进行断言
        :param args:
        :param assert_data:
        :param context:
        :return:
        '''
        try:
            for element in self.kw.find_elements(args=args, context=None):
                assert assert_data in element.text, '断言失败，当前页面页面存在筛选条件外的元素'
        except AssertionError as e:
            error_log().debug(e)
            raise e

    def assert_by_elements_no_exist(self, args, assert_data, context=None):
        '''
        根据元素是否存在进行断言
        :param args:
        :param assert_data:
        :param context:
        :return:
        '''
        try:
            # 如果元素存在，则说明操作失败
            self.kw.find_element(args=args, context=None)
            assert 1 == 2, '断言失败, 当前元素在页面当中存在'
        except Exception as e:
            assert 1 == 1, '断言成功, 页面数据不存在'

    def assert_by_toast(self, args, assert_data, context=None):
        try:
            # 如果元素存在，则说明操作失败
            text = self.kw.toast(args=args, context=context)
            assert assert_data in text, '断言失败，当前值不在toast当中'
        except Exception as e:
            error_log().debug(e)
            raise e
